﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace AutoWordInfo
{
    public partial class Setting : Form
    {

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        public Setting()
        {
            InitializeComponent();
            loadIni();
        }
        
        private void loadIni()
        {
            string m_strINIpath = Path.GetDirectoryName(Application.ExecutablePath) + @"\setting.ini";

            StringBuilder strBdRetn = new StringBuilder();


            GetPrivateProfileString("DB", "IP", "", strBdRetn, 255, m_strINIpath);
            this.IPtextBox.Text = strBdRetn.ToString();
            GetPrivateProfileString("DB", "Login", "", strBdRetn, 255, m_strINIpath);
            this.UsertextBox.Text = strBdRetn.ToString();
            GetPrivateProfileString("DB", "PWD", "", strBdRetn, 255, m_strINIpath);
            this.PWDtextBox.Text = strBdRetn.ToString();

            GetPrivateProfileString("DB", "DATABASE", "", strBdRetn, 255, m_strINIpath);
            this.DBtextBox.Text = strBdRetn.ToString();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            string ip = this.IPtextBox.Text;
            string db = this.DBtextBox.Text;
            string user = this.UsertextBox.Text;
            string pwd = this.PWDtextBox.Text;

            if (ip.Equals(""))
            {
                MessageBox.Show("请输入服务器IP", "提示");
                return;
            }

            if (db.Equals(""))
            {
                MessageBox.Show("请输入数据库名称", "提示");
                return;
            }
            if (user.Equals(""))
            {
                MessageBox.Show("请输入数据库登录用户名", "提示");
                return;
            }
            if (pwd.Equals(""))
            {
                MessageBox.Show("请输入数据库密码", "提示");
                return;
            }
            saveInfo(ip, db, user, pwd);
            MessageBox.Show("保存完成", "提示");
            this.Close();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            string ip = this.IPtextBox.Text;
            string db = this.DBtextBox.Text;
            string user = this.UsertextBox.Text;
            string pwd = this.PWDtextBox.Text;
            MysqlServer mysql = new MysqlServer();
            if (ip.Equals(""))
            {
                MessageBox.Show("请输入服务器IP", "提示");
                return;
            }
            if (db.Equals(""))
            {
                MessageBox.Show("请输入数据库名称", "提示");
                return;
            }
            if (user.Equals(""))
            {
                MessageBox.Show("请输入数据库登录用户名", "提示");
                return;
            }
            if (pwd.Equals(""))
            {
                MessageBox.Show("请输入数据库密码", "提示");
                return;
            }

            int result = mysql.connectMysql(ip, user, pwd, db);
            if (result == 0)
            {
                MessageBox.Show("mysql链接失败,请检查数据库名称是否正确", "提示");
                return;
            }
            if (result == -1)
            {
                MessageBox.Show("mysql链接失败,用户名和密码是否正确", "提示");
                return;
            }
            if (result == 1)
            {
                MessageBox.Show("数据库连接成功", "提示");
                return;
            }
            else
            {
                MessageBox.Show("未知错误，请联系技术人员", "提示");
                return;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void saveInfo(string ip, string db, string user, string pwd)
        {

            string m_strINIpath = Path.GetDirectoryName(Application.ExecutablePath) + @"\setting.ini";
            WritePrivateProfileString("DB", "DATABASE", db, m_strINIpath);
            WritePrivateProfileString("DB", "Login", user, m_strINIpath);
            WritePrivateProfileString("DB", "PWD", pwd, m_strINIpath);
            WritePrivateProfileString("DB", "IP", ip, m_strINIpath);


        }
    }
}
